package com.gamerole.app.ui

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.gamerole.app.R
import com.gamerole.app.databinding.AppFragmentHomeBinding
import com.gamerole.app.viewmodel.HomeViewModel
import com.gamerole.commom.adapter.MyFragmentPagerAdapter
import com.gamerole.commom.base.BaseFragment
import com.gamerole.commom.entity.TabItem
import com.gyf.immersionbar.ImmersionBar
import com.hi.dhl.binding.viewbind
import com.jeremyliao.liveeventbus.LiveEventBus
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
class HomeFragment : BaseFragment(R.layout.app_fragment_home) {

    private val viewModelHome: HomeViewModel by viewModels()
    override fun getViewModel() = viewModelHome
    private val binding: AppFragmentHomeBinding by viewbind()
    override fun initStatusNavBar() {
        ImmersionBar.with(this)
            .fitsSystemWindows(false)
            .fullScreen(true)
            .navigationBarDarkIcon(true)
            .statusBarDarkFont(true)
            .transparentNavigationBar()
            .init()
    }


    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            initStatusNavBar()
        }

    }

    private lateinit var fragments: ArrayList<Fragment>
    override fun initView() {
        with(binding) {
            val tabItems = ArrayList<CustomTabEntity>()
            tabItems.add(
                TabItem(
                    "首页",
                    R.mipmap.app_icon_menu_first_unselect,
                    R.mipmap.app_icon_menu_first_select
                )
            )

            tabItems.add(
                TabItem(
                    "我的",
                    R.mipmap.app_icon_menu_third_unselect,
                    R.mipmap.app_icon_menu_third_select
                )
            )
            tabItems.add(
                TabItem(
                    "测试",
                    R.mipmap.app_icon_menu_third_unselect,
                    R.mipmap.app_icon_menu_third_select
                )
            )
            commonTabLayout.setTabData(tabItems)
            commonTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
                override fun onTabSelect(position: Int) {
                    viewPager.currentItem = position
                }

                override fun onTabReselect(position: Int) {}
            })
            commonTabLayout.showDot(0)
            fragments = ArrayList()
            fragments.add(
                FirstFragment()
            )
            fragments.add(
                MineFragment()
            )
            fragments.add(
                MineFragment()
            )
            viewPager.offscreenPageLimit=fragments.size
            viewPager.adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
            viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    commonTabLayout.currentTab = position

                }
            })
        }
    }

    override fun initData() {
    }
}